<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>区块链产品消费交易众筹平台-项目支持</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/statics/layui/css/layui.css" media="all">
  <style>
    .layui-table-cell{
      height: 31px;
      line-height: 31px;
    }
  </style>
</head>

<body>
  <table class="layui-hide" id="dg" lay-filter="dg"></table>
  <!-- <script type="text/html" id="bar">
    <a class="layui-btn layui-btn-sm" lay-event="buy">支持</a>
  </script> -->
  <script type="text/html" id="bar">
    {{#  
      var userMoney = ProjectBuy.formatUserMoney(d.typeId);
      if(userMoney > 0 && userMoney >= d.min && d.money < d.allMoney){ }}
      <a class="layui-btn layui-btn-sm" lay-event="buy">支持</a>
    {{#  } else { }}
      <a class="layui-btn layui-btn-sm layui-btn-disabled">支持</a>
    {{#  } }}  
  </script>

  <script src="/statics/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="js/config.js"></script>
  <script type="text/javascript" src="js/utils.js"></script>
  <script>
    var layer;
    var model = null, typeModel = {};
    $(function () {
      ProjectBuy.init();
    });

    var ProjectBuy = {
      "UserMoneyData":[],
      "init":function(){
        if (parent && parent.id) {
          this.initUserMoney(function(){
            ProjectBuy.initInfo();
          });
        }
      },
      "initInfo":function(){
        DataUtils.getProjectInfo(window.parent.id, function (data) {
          model = data;
          layui.use(['table', 'layer', 'element'], function () {
            var table = layui.table;
            layer = layui.layer;
            table.render({
              elem: '#dg',
              cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
              cols: [[
                { field: 'id', title: 'ID', hide: true },
                { field: 'typeName', title: '币种', width: 80 },
                { field: 'money', title: '已投数/目标数', width: 130, templet:function(d){return d.money+'/'+d.allMoney }},
                { field: 'moneyScale', title: '比例', templet:function(d){return '1'+d.typeName+'='+d.moneyScale+model.moneyTypeName }},
                { field: 'money', title: '可投额度', width: 90, templet:function(d){return d.min+'-'+d.max }},
                { field: 'money', title: '我的余额', width: 90, templet:function(d){  return ProjectBuy.formatUserMoney(d.typeId) } },
                { fixed: 'right', title: '操作', toolbar: '#bar', width: 75 }
              ]],
              id: 'dg',
              page: false, loading: true, data: model.projectMoneys
            });

            //监听行工具事件
            table.on('tool(dg)', function (obj) {
              typeModel = obj.data;
              if (obj.event === 'buy') {
                if ((typeModel.allMoney-typeModel.money) <= 0) {
                  layer.alert('该币种投资已完成！', { title: '出错了', icon: 5 });
                } else {
                  var t = new Date().getTime();
                  UIUtils.openWindow('我要支持', 'projectbuyadd.html?t='+t+'&id=' + data.id, 450, 370);
                    // title: "支持币种 " + typeModel.typeName + " 不能超过 "+typeModel.leaveMoney+"",
                    // content: '<form class="layui-form" action="">'+
                    //           '<div class="layui-form-item">'+
                    //             '<div class="layui-inline">'+
                    //               '<label class="layui-form-label"><span class="layui-required">*</span> 支持数量</label>'+
                    //               '<div class="layui-input-inline" style="width: 100px;">'+
                    //                 '<input id="money" type="text" name="money" lay-verify="required|number" placeholder="￥" autocomplete="off" class="layui-input">'+
                    //               '</div>'+
                    //             '</div>'+
                    //           '</div>'+
                    //         '</form>'
                    // ,btn: ['确定', '取消']
                    // ,btnAlign: 'c'
                    // ,yes: function(index, layero){
                    //   var money = parseInt(layero.find("#money").val());
                    //   if(money<=0){
                    //     layer.alert("支持数量错误！", { title: '出错了', icon: 5 });
                    //     return false;
                    //   }
                    //   if(money>typeModel.leaveMoney){
                    //     layer.alert("支持数量不能超过"+typeModel.leaveMoney+"！", { title: '出错了', icon: 5 });
                    //     return false;
                    //   }
                    //   var param = {};
                    //   param.money = money;
                    //   param.typeId = typeModel.typeId;
                    //   param.projectId = typeModel.projectId;
                    //   DataUtils.buyProject(param, function(result, success){
                    //     layer.close();
                    //     if(success){
                    //       layer.alert('恭喜您，支持成功！', { title: '提示' }, function (index) {
                    //         window.top.location.reload();
                    //       });
                    //     }else{
                    //       layer.alert(result.resultMsg, { title: '出错了', icon: 5 });
                    //     }
                    //   });
                    // }
                  // });
                }
              }
            });
          });
        });
      },
      "initUserMoney":function(callback){
        DataUtils.getUserMoneyList(function(data){
          ProjectBuy.UserMoneyData = data;
          if(callback){
            callback(data);
          }
        });
      },
      "formatUserMoney":function(typeId){
        for(var i=0;i<ProjectBuy.UserMoneyData.length;i++){
          var item = ProjectBuy.UserMoneyData[i];
          if(item.typeId == typeId){
            return item.money;
          }
        }
        return 0;
      }
    };
  </script>

</body>

</html>